﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Service
{
    public class DeleteService
    {
        /// <summary>
        /// 删除预约单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceResult DeleteBookingBill(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_BookingBill set Status = {0} where Id={1}", Constant.BookingBillStatus.Terminated, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteBookingBill error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// 删除服务商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceResult DeleteBusiness(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Business set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteBusiness error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteBusinessArray(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Business set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteBusinessArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// 删除门店
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceResult DeleteStation(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Station set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteStation error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteStationArray(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Station set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteStationArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// 删除派工单
        /// </summary>
        /// <param name="maintenanceBillId"></param>
        /// <param name="maintenanceBillServiceItemId"></param>
        /// <returns></returns>
        public ServiceResult DeleteWorkingBill(Guid maintenanceBillId, Guid maintenanceBillServiceItemId)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EWorkingBill workingBill = dbContext.Set<Entity.EWorkingBill>().Where(p => p.MaintenanceBillId == maintenanceBillId
                    && p.MaintenanceBillServiceItemId == maintenanceBillServiceItemId
                    && p.Status != Constant.WorkingBillStatus.Deleted).SingleOrDefault();
                if (workingBill == null)
                {
                    return new ServiceResult(1, "workingbill not exist");
                }
                Entity.EMaintenanceBill maintenanceBill = dbContext.Set<Entity.EMaintenanceBill>().SingleOrDefault(p => p.Id == workingBill.MaintenanceBillId); 
                if (maintenanceBill == null)
                {
                    return new ServiceResult(1, "maintenanceBill not exist");
                }
                workingBill.Status = Constant.WorkingBillStatus.Deleted;
                dbContext.Entry<Entity.EWorkingBill>(workingBill).State = System.Data.Entity.EntityState.Modified;

                // 修改服务单对应的服务项目的状态为未派工
                Entity.EMaintenanceBillServiceItem serviceItem = dbContext.Set<Entity.EMaintenanceBillServiceItem>().SingleOrDefault(p => p.Id == workingBill.MaintenanceBillServiceItemId);
                serviceItem.Status = Constant.MaintenanceBillServiceItemStatus.Wait;

                if (maintenanceBill.MaterialCount == 0)
                {
                    // 无物料
                    if (maintenanceBill.ServiceItemCount == 1)
                    {
                        // 只有一个服务项目
                        maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Wait;
                        maintenanceBill.Status = Constant.MaintenanceBillStatus.WaitDispatching;
                    }
                    else
                    {
                        if (dbContext.Set<Entity.EWorkingBill>().Where(p => p.Status != Constant.WorkingBillStatus.Deleted &&
                            p.MaintenanceBillId == maintenanceBillId && p.MaintenanceBillServiceItemId != maintenanceBillServiceItemId).Count() > 0)
                        {
                            maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Working;
                            maintenanceBill.Status = Constant.MaintenanceBillStatus.Working;
                        }
                        else
                        {
                            maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Wait;
                            maintenanceBill.Status = Constant.MaintenanceBillStatus.WaitDispatching;
                        }
                    }
                }
                else
                {
                    // 有物料
                    if (maintenanceBill.ServiceItemCount == 1)
                    {
                        // 只有一个服务项目
                        maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Wait;
                        if (maintenanceBill.MaterialStatus == Constant.MaintenanceBillStatus.MaterialStatus.Wait)
                        {
                            maintenanceBill.Status = Constant.MaintenanceBillStatus.WaitDispatching;
                        }
                        else
                        {
                            maintenanceBill.Status = Constant.MaintenanceBillStatus.Working;
                        }
                    }
                    else
                    {
                        if (dbContext.Set<Entity.EWorkingBill>().Where(p => p.Status != Constant.WorkingBillStatus.Deleted &&
                            p.MaintenanceBillId == maintenanceBillId && p.MaintenanceBillServiceItemId != maintenanceBillServiceItemId).Count() > 0)
                        {
                            maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Working;
                            maintenanceBill.Status = Constant.MaintenanceBillStatus.Working;
                        }
                        else
                        {
                            maintenanceBill.ServiceItemStatus = Constant.MaintenanceBillStatus.ServiceItemStatus.Wait;
                            if (maintenanceBill.MaterialStatus == Constant.MaintenanceBillStatus.MaterialStatus.Wait)
                            {
                                maintenanceBill.Status = Constant.MaintenanceBillStatus.WaitDispatching;
                            }
                            else
                            {
                                maintenanceBill.Status = Constant.MaintenanceBillStatus.Working;
                            }
                        }
                    }
                }
                dbContext.Entry<Entity.EMaintenanceBillServiceItem>(serviceItem).State = System.Data.Entity.EntityState.Modified;

                dbContext.Entry<Entity.EMaintenanceBill>(maintenanceBill).State = System.Data.Entity.EntityState.Modified;
                // 修改服务单中的服务项目状态
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteWorkingBill error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteMaterialRequisitionBill(Guid maintenanceBillId, Guid maintenanaceBillMaterialId)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EMaterialRequisitionBill bill = dbContext.Set<Entity.EMaterialRequisitionBill>().Where(p => p.MaintenanceBillId == maintenanceBillId
                    && p.MaintenanceBillMaterialId == maintenanaceBillMaterialId
                    && p.Status == Constant.CommonStatus.Enabled).SingleOrDefault();
                if (bill == null)
                {
                    return new ServiceResult();
                }

                dbContext.Entry<Entity.EMaterialRequisitionBill>(bill).State = System.Data.Entity.EntityState.Deleted;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteMaterialRequisitionBill error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeletePayItem(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EPayItem entity = dbContext.Set<Entity.EPayItem>().Where(p => p.Id == id && p.Status != Constant.CommonStatus.Deleted).SingleOrDefault();
                if (entity == null)
                {
                    return new ServiceResult();
                }

                entity.Status = Constant.CommonStatus.Deleted;
                dbContext.Entry<Entity.EPayItem>(entity).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeletePayItem error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeletePayItemArray(Guid[] idArray)
        {
            if (idArray == null || idArray.Length == 0)
            {
                return new ServiceResult(1, "no id");
            }
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.AppendFormat("update ASMS_PayItem set [Status] = {0} where Id in (", Constant.CommonStatus.Deleted);
            for (int index = 0; index < idArray.Length; index++)
            {
                if (index == 0)
                {
                    sqlBuilder.AppendFormat("'{0}'", idArray[index]);
                }
                else
                {
                    sqlBuilder.AppendFormat(",'{0}'", idArray[index]);
                }
            }
            sqlBuilder.Append(")");
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand(sqlBuilder.ToString());
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeletePayItemArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeletePackage(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EPackage entity = dbContext.Set<Entity.EPackage>().Where(p => p.Id == id && p.Status != Constant.CommonStatus.Deleted).SingleOrDefault();
                if (entity == null)
                {
                    return new ServiceResult();
                }

                entity.Status = Constant.CommonStatus.Deleted;
                dbContext.Entry<Entity.EPackage>(entity).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeletePackage error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeletePackageArray(Guid[] idArray)
        {
            if (idArray == null || idArray.Length == 0)
            {
                return new ServiceResult(1, "no id");
            }
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.AppendFormat("update ASMS_Package set [Status] = {0} where Id in (", Constant.CommonStatus.Deleted);
            for (int index = 0; index < idArray.Length; index++)
            {
                if (index == 0)
                {
                    sqlBuilder.AppendFormat("'{0}'", idArray[index]);
                }
                else
                {
                    sqlBuilder.AppendFormat(",'{0}'", idArray[index]);
                }
            }
            sqlBuilder.Append(")");
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand(sqlBuilder.ToString());
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeletePackageArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// 删除库房
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceResult DeleteWarehouse(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Warehouse set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteWarehouse error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteWarehouseArray(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Warehouse set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteWarehouseArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// 删除服务项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceResult DeleteServiceItem(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteServiceItem error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteServiceItemArray(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteServiceItemArray error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteMaterial(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Material set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteMaterial error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteMaterialMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Material set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteMaterialMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteSupplier(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteSupplier error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult DeleteSupplierMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set Status = {0} where Id={1}", Constant.CommonStatus.Deleted, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("DeleteSupplierMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }
    }
}
